﻿using log4net;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JT809.GpsRedisConsumer.Common
{
    public class WriteToRedis
    {
        private readonly static ILog _logger = LogManager.GetLogger(LogHelper.Repository.Name, typeof(WriteToRedis));

        /// <summary>
        /// 错误或各类信息列表
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="Msg">错误内容</param>
        /// <param name="day">保存多少天</param>
        /// <returns></returns>
        public static async Task VariousAsync(string Key, string Msg, int day = 30)
        {
            try
            {
                DateTime dnt = DateTime.Now;
                //只保留按当前时间往前一段时间以内的数据
                await RedisHelper.ZRemRangeByScoreAsync(Key, 0, DateTimeUtil.DateTimeToLongTimeStamp(dnt.AddDays(-day)));
                //插入ZSet，按照时间戳排序，时间越后，分值越高
                await RedisHelper.ZAddAsync(Key, (DateTimeUtil.DateTimeToLongTimeStamp(dnt), $"【{dnt.ToString("yyyy-MM-dd HH:mm:ss")}】【{Msg}】"));
            }
            catch (Exception exx)
            {
                _logger.Info($"【错误】【{Key}】【{exx.Message}】");
            }
        }

        /// <summary>
        /// 设备上传的信息列表
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="Msg">设备的上传信息</param>
        /// <param name="day">保存多少天</param>
        /// <returns></returns>
        public static async Task OnlineInfosync(string Key, string Msg, int day = 7)
        {
            Key = $"OnlineInfo:{Key}";
            try
            {
                DateTime dnt = DateTime.Now;
                //只保留按当前时间往前一段时间以内的数据
                await RedisHelper.ZRemRangeByScoreAsync(Key, 0, DateTimeUtil.DateTimeToLongTimeStamp(dnt.AddDays(-day)));
                //插入ZSet，按照时间戳排序，时间越后，分值越高
                await RedisHelper.ZAddAsync(Key, (DateTimeUtil.DateTimeToLongTimeStamp(dnt), $"{Msg}"));
            }
            catch (Exception exx)
            {
                _logger.Info($"【错误】【{Key}】【{exx.Message}】");
            }
        }

        /// <summary>
        /// 在线的设备清单
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="Id">设备唯一标识</param>
        /// <param name="day">保存多少天</param>
        /// <returns></returns>
        public static async Task OnlineAsync(string Key, string Id, int day = 7)
        {
            Key = $"Online:{Key}";
            try
            {
                DateTime dnt = DateTime.Now;
                //只保留按当前时间往前一段时间以内的数据
                await RedisHelper.ZRemRangeByScoreAsync(Key, 0, DateTimeUtil.DateTimeToLongTimeStamp(dnt.AddDays(-day)));
                //插入ZSet，按照时间戳排序，时间越后，分值越高
                await RedisHelper.ZAddAsync(Key, (DateTimeUtil.DateTimeToLongTimeStamp(dnt), $"{Id}"));
            }
            catch (Exception exx)
            {
                _logger.Info($"【错误】【{Key}】【{exx.Message}】");
            }
        }

        /// <summary>
        /// 所有上传过信息的设备清单
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="Id">设备唯一标识</param>
        /// <returns></returns>
        public static async Task AllAsync(string Key, string Id)
        {
            Key = $"All:{Key}";
            try
            {
                await RedisHelper.SAddAsync(Key, Id);
            }
            catch (Exception exx)
            {
                _logger.Info($"【错误】【{Key}】【{exx.Message}】");
            }
        }

        /// <summary>
        /// 设备的心跳列表
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="Id"></param>
        /// <param name="hour"></param>
        /// <returns></returns>
        public static async Task HeartBeatAsync(string Key, string Id, int hour = 2)
        {
            Key = $"HeartBeat:{Key}";
            try
            {
                DateTime dnt = DateTime.Now;
                //只保留按当前时间往前一段时间以内的数据
                await RedisHelper.ZRemRangeByScoreAsync(Key, 0, DateTimeUtil.DateTimeToLongTimeStamp(dnt.AddHours(-hour)));
                //插入ZSet，按照时间戳排序，时间越后，分值越高
                await RedisHelper.ZAddAsync(Key, (DateTimeUtil.DateTimeToLongTimeStamp(dnt), $"{Id}"));
            }
            catch (Exception exx)
            {
                _logger.Info($"【错误】【{Key}】【{exx.Message}】");
            }
        }

        /// <summary>
        /// 设备最近一次上传的信息实体
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="job">信息实体的jobject对象</param>
        /// <returns></returns>
        public static async Task ObjectAsync(string Key, JObject job)
        {
            Key = $"Object:{Key}";
            try
            {
                var list = new List<string>();
                foreach (var item in job)
                {
                    list.Add(item.Key);
                    list.Add(item.Value.ToString());
                }
                await RedisHelper.HMSetAsync(Key, list.ToArray());
            }
            catch (Exception exx)
            {
                _logger.Info($"【错误】【{Key}】【{exx.Message}】");
            }
        }

        /// <summary>
        /// 数据插入队列
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static async Task ListAsync(string Key, string value)
        {
            try
            {
                await RedisHelper.RPushAsync(Key, value);
            }
            catch (Exception exx)
            {
                _logger.Info($"【错误】【{Key}】【{exx.Message}】");
            }
        }
    }
}
